package br.com.ifba.sadd.model {
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.remoting.RemoteObject;

	[Bindable]
	public class ServiceLocator
	{
		
		private const DESTINATION:String = "amfphp";
		private const ENDPOINT:String = "../amfphp/index.php";
		private const ALUNO_SERVICE:String = "AlunoService";
		private const CURSO_SERVICE:String = "CursoService";		
		private const DISCIPLINA_SERVICE:String = "DisciplinaService";
		private const MODALIDADE_SERVICE:String = "ModalidadeService";		
		private const PROFESSOR_SERVICE:String = "ProfessorService";
		private const TURMA_SERVICE:String = "TurmaService";
		
		private static var _channelAMF:AMFChannel = null;
		private static var _instance:ServiceLocator = null;		
		private static var _remoteObject:RemoteObject = null;
		
		private var _alunoService:RemoteObject;
		private var _cursoService:RemoteObject;
		private var _disciplinaService:RemoteObject;
		private var _modalidadeService:RemoteObject;		
		private var _professorService:RemoteObject;
		private var _turmaService:RemoteObject;
		
		public function ServiceLocator(enforcer:SingletonEnforcer)
		{
			if(enforcer == null){
				throw new Error("Erro ao criar o Facade");
			}
			else{
				_channelAMF = new AMFChannel(DESTINATION, ENDPOINT);
				_remoteObject = new RemoteObject(DESTINATION);
				_remoteObject.channelSet = new ChannelSet();
				_remoteObject.channelSet.addChannel(_channelAMF);
			}
		}
		
		public static function getInstance():ServiceLocator
		{
			if(_instance == null)
			{
				_instance= new ServiceLocator(new SingletonEnforcer);
			}
			return _instance;
		}

		public function get alunoService():RemoteObject
		{
			_alunoService = _remoteObject;
			_alunoService.source = ALUNO_SERVICE;
			_alunoService.showBusyCursor = true;
			return _alunoService;
		}
		
		public function get cursoService():RemoteObject
		{
			_cursoService = _remoteObject;
			_cursoService.source = CURSO_SERVICE;
			_cursoService.showBusyCursor = true;
			return _cursoService;
		}

		public function get modalidadeService():RemoteObject
		{
			_modalidadeService = _remoteObject;
			_modalidadeService.source = MODALIDADE_SERVICE;
			_modalidadeService.showBusyCursor = true;
			return _modalidadeService;
		}
		
		public function get disciplinaService():RemoteObject
		{
			_disciplinaService = _remoteObject;
			_disciplinaService.source = DISCIPLINA_SERVICE;
			_disciplinaService.showBusyCursor = true;
			return _disciplinaService;
		}
		
		public function get professorService():RemoteObject
		{
			_professorService = _remoteObject;
			_professorService.source = PROFESSOR_SERVICE;
			_professorService.showBusyCursor = true;
			return _professorService;
		}
		
		public function get turmaService():RemoteObject
		{
			_turmaService = _remoteObject;
			_turmaService.source = TURMA_SERVICE;
			_turmaService.showBusyCursor = true;
			return _turmaService;
		}
				
	}
}

/**
 * Bloqueia o acesso ao construtor da classe SaddFacade
 * fazendo com que ela seja instanciada apenas por ela mesma 
 * garantindo assim unicidade do objeto desta classe no sistema
 * */
class SingletonEnforcer {}